/*******************************************************************************
 * 创建: 2024年08月28日
 * 作者: 水煮肉片饭(27185709@qq.com)
 * 描述: 拖拽器，游戏中可以按住该节点进行拖拽
*******************************************************************************/
import * as cc from 'cc';
const { ccclass, menu } = cc._decorator;
@ccclass
@menu('Public/Dragger')
export class Dragger extends cc.Component {
    prevPos: cc.Vec2 = new cc.Vec2(0, 0);
    protected onEnable() {
        this.node.on(cc.NodeEventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.NodeEventType.TOUCH_MOVE, this.onTouchMove, this);
    }
    protected onDisable() {
        this.node.off(cc.NodeEventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(cc.NodeEventType.TOUCH_MOVE, this.onTouchMove, this);
    }
    private onTouchStart(event: cc.EventTouch) {
        this.prevPos = event.getUILocation();
    }
    private onTouchMove(event: cc.EventTouch) {
        let pos = this.node.position;
        let curPos = event.getUILocation();
        let prevPos = this.prevPos;
        this.node.setPosition(pos.x + curPos.x - prevPos.x, pos.y + curPos.y - prevPos.y);
        this.prevPos = curPos;
    }
}